// const db = uniCloud.database();
// const utils = require('self-utils')
// module.exports = {
// 	_before: function () { // 通用预处理器

// 	},
// 	async add(data){
// 		let userInfo = await utils.getUserInfo(this);
// 		data.user_id = userInfo.uid
// 		data.creatTime = Date.now()
// 		if(data.selected){
// 			await db.collection("wanshop-mall-address").where({
// 				user_id:userInfo.uid
// 			}).update({
// 				selected:false
// 			})
// 		}	
// 		return await db.collection("wanshop-mall-address").add(data)

// 	}

// }

const db = uniCloud.database();
const utils = require('self-utils');

module.exports = {
	_before: async function() { // 通用预处理器
		this.userInfo = await utils.getUserInfo(this);
	},

	async add(data) {
		try {
			if (!this.userInfo) {
				throw new Error('用户信息获取失败');
			}
			data.user_id = this.userInfo.uid;
			data.creatTime = Date.now();
			if (data.selected) {
				await db.collection("wanshop-mall-address").where({
					user_id: this.userInfo.uid
				}).update({
					selected: false
				});
			}

			return await db.collection("wanshop-mall-address").add(data);
		} catch (error) {
			console.error('添加地址失败:', error);
			throw new Error('添加地址失败');
		}
	},
	// async getList() {
	// 	try {
	// 		if (!this.userInfo) {
	// 			throw new Error('用户信息获取失败');
	// 		}
	// 		const res =  await db.collection("wanshop-mall-address").where({
	// 			user_id: this.userInfo.uid
	// 		}).orderBy("selected","desc").orderBy("creatTime","desc").get();
	// 		return res;
	// 	} catch (error) {
	// 		console.error('获取地址失败:', error);
	// 		throw new Error('获取地址失败');
	// 	}
	// },
	
	async getList(){
		if(!this.userInfo.uid) return this.userInfo;
		return await db.collection("wanshop-mall-address").where({
			user_id: this.userInfo.uid
		}).orderBy("selected","desc").orderBy("creatTime","desc").get()
	},
	// 修改默认地址
	async setDefault(id) {
	    try {
	      if (!this.userInfo || !this.userInfo.uid) {
	        throw new Error('用户信息获取失败');
	      }
	
	      // 更新其他地址的 selected 为 false
	      await db.collection("wanshop-mall-address").where({
	        user_id: this.userInfo.uid
	      }).update({
	        selected: false
	      });
	
	      // 将目标地址的 selected 设置为 true
	      const res = await db.collection("wanshop-mall-address").doc(id).update({
	        selected: true
	      });
	
	      // 返回成功信息
	      return {
	        success: true,
	        message: '默认地址已更新'
	      };
	    } catch (error) {
	      console.error('设置默认地址失败:', error);
	      throw new Error('设置默认地址失败');
	    }
	  },
	  async getOne(id){
		  return await db.collection("wanshop-mall-address").doc(id).get();
	  },
	  // 修改单挑地址记录
	  async updateOne(data){
		  try {
		  	if (!this.userInfo) {
		  		throw new Error('用户信息获取失败');
		  	}
		  	data.user_id = this.userInfo.uid;
		  	if (data.selected) {
		  		await db.collection("wanshop-mall-address").where({
		  			user_id: this.userInfo.uid
		  		}).update({
		  			selected: false
		  		});
		  	}
			let params = {...data};
			delete params._id
		  
		  	return await db.collection("wanshop-mall-address").doc(data._id).update(params);
		  } catch (error) {
		  	console.error('修改地址失败:', error);
		  	throw new Error('修改地址失败');
		  }
	  },
	 async getDefAddress(){
		 if (!this.userInfo) {
		 	throw new Error('用户信息获取失败');
		 }
		 return await db.collection("wanshop-mall-address").where({
			 user_id: this.userInfo.uid,
			 selected:true
		 }).get()
	  }
};